<?php

namespace app\admin\controller;

use app\admin\model\Admin as adminModel;
use think\captcha\Captcha;
use think\Controller;
use think\Db;
use think\Request;

class Admin extends Controller
{


    //后台登陆页面显示
    public function index()
    {


        return $this->fetch('login');
    }

    //后台登陆用户提交的数据  post方法
    public function Login(Request $request)
    {
//    	//接收验证码和token
//    	$data=$request->only(['__token__']);
//
//    	//指定验证规则
//    	$rule=['code|验证码'=>'captcha|token'];
//
//    	//引用validate 并实例化validate
//    	$validate=new Validate();
//    	//判断
//    	if(!$validate->check($data,$rule)){
//    		return json(['status'=>'fail','msg'=>$validate->getError()]);  ;
//    	}

        $user_name = $request->param('user_name', 'trim');
        $password = $request->param('password', 'trim');

        $adminModel = new adminModel;

        $res = $adminModel->checkAdmin($user_name, $password);

        if ($res['status'] == 'success') {
            session('admin_name', $user_name);
            $this->success('登录成功', '/common');
        } else {
            $this->error('登录失败', '/admin/index');
        }
        return json($res);
    }

    //用户退出
    public function logout()
    {
        session('admin_name', null);
        return $this->redirect('/admin/index');
    }

    //添加管理员
    public function add(Request $request)
    {
        if ($request->ispost()) {
            $user_name = $request->param('user_name', 'trim');
            $password = $request->param('password', 'trim');
            $type = $request->param('type', 'trim');
            $realname = $request->param('realname', 'trim');
            $salt = mt_rand(100000, 999999);
            $password = md5($password . $salt);
            $data = ['username' => $user_name, 'password_hash' => $password, 'type'=>$type,'salt' => $salt, 'realname' => $realname, 'created_at' => time()];

            $admin=new  adminModel;
            $res=$admin->insertUser($data);
            if($res){
                 $this->success('注册成功', 'admin/Admin/list');;
            }else{
                 $this->success('注册失败', 'admin/Admin/list');;
            }
        }else {

            return $this->fetch('add');
        }
    }
    //管理员列表
    public function list(){

        $admin=new  adminModel;
        $admin=$admin->adminList();

        $this->assign('admin',$admin);

        return $this->fetch('list');

    }
    //管理员删除
    public function delete($id){
        $admin=new  adminModel;
        $admin=$admin->deleteAdmin($id);
        if($admin){
            return json(['status'=>'success','msg'=>'删除成功']);
        }else{
            return json(['status'=>'fail','msg'=>'删除失败' ]);
        }
    }
    //检查用户是否已经注册
//    public function check(Request $request){
//        echo 53434;exit;
//        $username=$request->param('param','trim');
//        //实例化
//        $userModel=new userModel;
//        $res=$userModel->checkUser($mobile);
//        ////接收model返回过来的数据并判断
//        if($res){
//            return json(['status'=>'n','info'=>'此手机已经注册']);
//        }else{
//            return json(['status'=>'y','info'=>'此手机可以注册']);
//        }
//    }




}
